package com.qk.controller;

import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

//    @PostMapping("/upload")
//    public Result upload(String name , Integer age , MultipartFile  file) throws Exception {
//        log.info("上传文件{}",file);
//        String filename =file.getOriginalFilename();//获取源文件名字
//        String name1 = filename.substring(filename.lastIndexOf("."));//以最后一个点切割
//        String newFileName = UUID.randomUUID().toString()+name1;//通过UUID随机数生成新的文件名+文件后缀
//        file.transferTo(new File("D:\\image\\"+newFileName));
//        return Result.success();
//    }
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        String originalFilename = image.getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + substring;
        String s = aliyunOSSOperator.upload(image.getBytes(), newFileName);
        return Result.success(s);
    }
}
